# GRAPHS FOR ANALYSIS

# Install and load packages if not already done

packages <- c("plyr", "broom", "gridExtra", "ggplot2", "zoo", "scales", "stringr", "dplyr", "tidyverse", "text2vec", 
              "lubridate", "data.table", "quanteda", "quanteda.textstats", "magrittr", 
              "fixest", "modelsummary", "marginaleffects", "texreg")
install_and_load <- function(package) {
  if (!require(package, character.only = TRUE)) {
    install.packages(package, dependencies = TRUE)
    library(package, character.only = TRUE)
  }
}
invisible(lapply(packages, install_and_load))

# Load data

setwd("FOLDER PATH")
load('df_ft_all_final.rda')

# Making aggregated df

df_aggregated <- df_ft_all %>%
  group_by(year) %>%
  summarize(
    arbejdsløshedsstøtte = sum(arbejdsløshedsstøtte, na.rm = TRUE),
    arbejdsdag = sum(arbejdsdag, na.rm = TRUE),
    dyrtid = sum(dyrtid, na.rm = TRUE),
    merindkomstskat = sum(merindkomstskat, na.rm = TRUE),
    repress = sum(repress, na.rm = TRUE),
    socrevall = sum(socrevall, na.rm = TRUE)
  )

# FIGURE 3-5 - PLOTS

# Unemployment support

p <- ggplot(df_aggregated, aes(x = year, y = arbejdsløshedsstøtte)) +
  geom_line() +
  scale_x_continuous(breaks = seq(from = 1910, to = 1930, by = 2)) +
  #coord_cartesian(ylim=c(0, 5000))  +
  theme_minimal()
arbst_plot <- arbdag_plot <- dyrtid_plot <- p + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  ylab("Unemployment benefits")+
  xlab("Year")
arbst_plot
save(arbst_plot, file = "FOLDER PATH")


# Working day

p <- ggplot(df_aggregated, aes(x = year, y = arbejdsdag)) +
  geom_line() +
  scale_x_continuous(breaks = seq(from = 1910, to = 1930, by = 2)) +
  #coord_cartesian(ylim=c(0, 5000))  +
  theme_minimal()
arbdag_plot <- dyrtid_plot <- p + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  ylab("Working day")+
  xlab("Year")
arbdag_plot
save(arbdag_plot, file = "C:/Users/nholmes/Downloads/arbdag_plot.RData")


# Price increase compensation

p <- ggplot(df_aggregated, aes(x = year, y = dyrtid)) +
  geom_line() +
  scale_x_continuous(breaks = seq(from = 1910, to = 1930, by = 2)) +
  #coord_cartesian(ylim=c(0, 5000))  +
  theme_minimal()
dyrtid_plot <- p + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  ylab("Price increase compensation")+
  xlab("Year")

dyrtid_plot
save(dyrtid_plot, file = "C:/Users/nholmes/Downloads/dyrtid_plot.RData")

# Additional taxation

p <- ggplot(df_aggregated, aes(x = year, y = merindkomstskat)) +
  geom_line() +
  scale_x_continuous(breaks = seq(from = 1910, to = 1930, by = 2)) +
  #coord_cartesian(ylim=c(0, 5000))  +
  theme_minimal()
merindkomstskat_plot <- p + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  ylab("Additional taxation")+
  xlab("Year")
merindkomstskat_plot
save(merindkomstskat_plot, file = "C:/Users/nholmes/Downloads/merindkomstskat_plot.RData")

library(gridExtra)
policies_plot <- grid.arrange(arbst_plot, arbdag_plot, dyrtid_plot, merindkomstskat_plot, ncol = 2, nrow = 2)
ggsave("FOLDER PATH", plot = policies_plot, width = 8, height = 5)

# Repression

p <- ggplot(df_aggregated, aes(x = year, y = repress)) +
  geom_line() +
  scale_x_continuous(breaks = seq(from = 1910, to = 1930, by = 2)) +
  coord_cartesian(ylim=c(0, 750))  +
  theme_minimal()
repress_plot <- p + 
  theme(
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    axis.text = element_text(size = 14),
    axis.title.x = element_text(size = 14),  # Increase x-axis label size
    axis.title.y = element_text(size = 14)   # Increase y-axis label size
  ) +  ylab("Repression")+
  xlab("Year")
repress_plot
ggsave(repress_plot, file = "FOLDER PATH", width = 8, height = 5)

# Revolutionary terms

p <- ggplot(df_aggregated, aes(x = year, y = socrevall)) +
  geom_line() +
  scale_x_continuous(breaks = seq(from = 1910, to = 1930, by = 2)) +
  #coord_cartesian(ylim=c(0, 1200))  +
  theme_minimal()
socrevall_plot <- p + 
  theme(
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    axis.text = element_text(size = 14),
    axis.title.x = element_text(size = 14),  # Increase x-axis label size
    axis.title.y = element_text(size = 14)   # Increase y-axis label size
  ) +  ylab("Revolutionary terms")+
  xlab("Year")
socrevall_plot
ggsave(socrevall_plot, file = "FOLDER PATH", width = 8, height = 5)

# FIGURE 6 - REVOLUTIONARY TERMS AND COOPTATION/REPRESSION

# Revolutionary terms + cooptation (regression)

df_ft_all <- subset(df_ft_all, party != 1) # Removing speaker

df_ft_all$socrevall_coopt <- ifelse(df_ft_all$socrevall > 0 & (df_ft_all$arbejdsløshedsstøtte > 0 | df_ft_all$dyrtid > 0 | df_ft_all$arbejdsdag > 0 | df_ft_all$merindkomstskat > 0), 1, 0)
df_ft_all$y17_19 <- as.numeric(format(df_ft_all$date, '%Y-%m-%d') %between% c('1917-03-08', '1919-08-11'))
df_ft_all_y17_19 <- subset(df_ft_all, format(as.Date(date),"%Y-%m-%d")<'1919-08-12')
df_ft_all_y17_19$year <- as.numeric(df_ft_all_y17_19$year)

lmsocrevallcoopt17_19_inf <- feglm(
  socrevall_coopt~y17_19  + growth + unemp + election + inflation + n_tokens_all, 
  data = df_ft_all_y17_19,
  family = quasipoisson(),
  se = "hetero")

# Revolutionary terms + repression (regression)

df_ft_all$socrevall_repress <- ifelse(df_ft_all$socrevall > 0 & df_ft_all$repress > 0, 1, 0)
df_ft_all$y17_19 <- as.numeric(format(df_ft_all$date, '%Y-%m-%d') %between% c('1917-03-08', '1919-08-11'))
df_ft_all_y17_19 <- subset(df_ft_all, format(as.Date(date),"%Y-%m-%d")<'1919-08-12')
df_ft_all_y17_19$year <- as.numeric(df_ft_all_y17_19$year)

lmsocrevallrepress17_19_inf <- feglm(
  socrevall_repress ~ y17_19 + growth + unemp + election + inflation + n_tokens_all,
  data = df_ft_all_y17_19,
  family = quasipoisson(),
  se = "hetero")

# Plot, revolutionary terms + cooptation/repression

tidy_results <- bind_rows(
  tidy(lmsocrevallcoopt17_19_inf, conf.int = TRUE) %>% mutate(model = "Revolutionary terms and co-optation"),
  tidy(lmsocrevallrepress17_19_inf, conf.int = TRUE) %>% mutate(model = "Revolutionary terms and repression")
)

y17_19_results <- filter(tidy_results, term == "y17_19") # Filter rows related to y17_19

socrevall_cooptrepress <- ggplot(y17_19_results, aes(x = model, y = estimate, ymin = conf.low, ymax = conf.high)) + # Plot the coefficients with confidence intervals
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(position = position_dodge(width = 0.5), width = 0.2) +
  labs(x = "", y = "Estimate, revolutionary period") +
  theme_minimal()
ggsave("FOLDER PATH", plot = socrevall_cooptrepress, width = 6, height = 4)

# FIGURE 7 - WORDSCORES 

# Load and install packages
install.packages("quanteda")
remotes::install_github("quanteda/quanteda.textmodels")
remotes::install_github("quanteda/quanteda.textplots") 

library(dplyr)
library(quanteda)
library(quanteda.textplots)
library(quanteda.textmodels)

# Load Wordscores-models
setwd("FOLDER PATH")
load("/wordscores_combined.RData")
wordscores_combined <- pred_ws

# Create a dataframe
fit_names <- names(wordscores_combined$fit)
fit_values <- unname(wordscores_combined$fit)
se_fit_values <- rep(wordscores_combined$se.fit, each = length(fit_values) / length(fit_names))
ws_combined <- data.frame(Fit_Name = rep(fit_names, each = length(fit_values) / length(fit_names)),
                          Fit_Value = fit_values,
                          SE_Fit_Value = se_fit_values)
# Define the mapping of old values to new values
rename_mapping <- c("2" = "Social Democrats, 1910-15", "3" = "Conservatives, 1910-15", "4" = "Liberals, 1910-15", "5" = "Social-Liberals, 1910-15",
                    "215" = "Social Democrats, 1915-17", "315" = "Conservatives, 1915-17", "415" = "Liberals, 1915-17", "515" = "Social-Liberals, 1915-17",
                    "217" = "Social Democrats, revolutionary period", "317" = "Conservatives, revolutionary period", "417" = "Liberals, revolutionary period", "517" = "Social-Liberals, revolutionary period")
ws_combined$party <- factor(ws_combined$Fit_Name, levels = names(rename_mapping), labels = rename_mapping)

# Making figure

# Define the custom order
custom_order <- c("Social Democrats, 1910-15",
                  "Social Democrats, 1915-17",
                  "Social Democrats, revolutionary period",
                  "Conservatives, 1910-15",
                  "Conservatives, 1915-17",
                  "Conservatives, revolutionary period",
                  "Liberals, 1910-15",
                  "Liberals, 1915-17",
                  "Liberals, revolutionary period",
                  "Social-Liberals, 1910-15",
                  "Social-Liberals, 1915-17",
                  "Social-Liberals, revolutionary period")

# Reorder the party names based on the custom order
ws_combined$party <- factor(ws_combined$party, levels = rev(custom_order))

# Calculate upper and lower bounds for confidence intervals
ws_combined$lower_bound <- ws_combined$Fit_Value - 1.96 * ws_combined$SE_Fit_Value
ws_combined$upper_bound <- ws_combined$Fit_Value + 1.96 * ws_combined$SE_Fit_Value

# Create the plot
wordscores_plot <- ggplot(ws_combined, aes(y = party)) +
  geom_point(aes(x = Fit_Value), size = 1) +
  geom_errorbar(aes(xmin = lower_bound, xmax = upper_bound), width = 0.2) +
  labs(x = "Wordscore", y = "Party") +
  theme_minimal()

ggsave("FOLDER PATH", plot = wordscores_plot, width = 6, height = 6)
